//企业管理-员工管理-导入表格
App.util.ns('Admin.Enterprise.Personnel');
Admin.Enterprise.Personnel.MultiImport = function(dataTable){
    var multiImporter = {
        data: [],
        items: [
        ],
        frame: function(){
            var self = this;
            this.frame = $([
                '<div class="video-multi-upload-container">',
                '    <div class="add-btn-container">',
                '        <button class="btn btn-success btn-sm add-btn"><i class="fa fa-plus"></i> 导入企业员工名单</button>',
                '        <input class="upload-file" type="file" style="display: none;"/>',
                '    </div>',
                '</div>'
            ].join(''));
            //console.log(this);
            return this;
        },
        dataTable: function(){
            var self = this;
            return this;
        },
        init: function(){
            var self = this;
            self.frame().dataTable();
            //数据表格
            self.frame.prepend(self.dataTable.container);
            this.frame.find('.add-btn').click(function(){
                self.frame.find('.upload-file').click();
            });
            this.frame.find('.upload-file').change(function(){
                if(this.files.length > 0){
                    self.addFile(this.files[0]);
                }
            });
            return this;
        },
        addFile: function(file){
            if(file.size > 8388608){
                App.util.hint.alert({
                    text: '文件不能超过8Mb'
                });
                return;
            }
            var self = this;
            var enterpriseId = dataTable.param.data.enterprise_id;
            var xhr = new XMLHttpRequest();
            xhr.open('POST', dataTable.url('importCheck'), true);
            var formData, startDate = new Date().getTime();
            formData = new FormData();
            formData.append('file', file);
            formData.append('enterpriseId', enterpriseId);

            //进度
            var toast = App.util.hint.toast({
                type: 'loading',
                text: '正在上传数据，请稍后...',
                lock: true
            });
            //成功
            xhr.onreadystatechange = function() {
                if(xhr.readyState === 4) {
                    toast.close();
                    if(xhr.status !== 200){//网络错误
                        //错误提示
                        App.util.hint.toast({
                            type: 'error',
                            text: '服务器错误'
                        }).delay(3000);
                        return ;
                    }
                    var dataRaw = JSON.parse(xhr.responseText);
                    if(dataRaw.status === '0'){
                        App.util.hint.toast({
                            type: 'ok',
                            text: '导入成功，手动刷新'
                        }).delay(3000);
                        win.hide();
                    }else{//其它错误
                        //错误提示
                        App.util.hint.toast({
                            type: 'error',
                            text: dataRaw.msg
                        }).delay(3000);
                    }
                }
            };
            xhr.send(formData);
        },
    }.init();
    var win = new App.Win({name: 'multi_import', width: '800px'});
    win.setTitle('导入表格');
    win.setContent(multiImporter.frame);
    win.hide();
    return win;
};